package com.minisoft.utils.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author JueSu
 * @description: TODO
 * @date 2023/7/28 13:00
 */
public class ExportDaasResult {

    public static void main(String[] args) throws JsonProcessingException {
        String outputFilePath = "D:\\temp/daasResult-"+System.currentTimeMillis()+".xlsx";
        // Read JSON data from the input file
        String jsonData = FileUtil.readString(ExportDaasResult.class.getResource("/daasResult.txt"),"utf-8");

        // Parse JSON data to get rows and column_list
        JSONObject jsonObject = JSON.parseObject(jsonData);
        List<String> columnList = jsonObject.getJSONArray("column_list").toJavaList(String.class);
        List<List<String>> rows = new ObjectMapper().readValue(jsonObject.getJSONArray("rows").toString(), new TypeReference<List<List<String>>>() {});

        // Create Excel writer
        ExcelWriter writer = ExcelUtil.getWriter(outputFilePath);

        // Set the header row
        writer.writeHeadRow(columnList);

        // Write data rows
        writer.write(rows);

        // Close the writer and save the Excel file
        writer.close();

        System.out.println("done");
    }

}
